<?php


namespace app\api\controller\v1;
use app\api\model\v1\FirmCourse;
use app\api\model\v1\FirmCulture;
use app\api\model\v1\FirmInfo;
use app\api\model\v1\MechanismCourse;
use app\api\model\v1\VipOrder;
use app\api\model\v1\VipPrice;
use app\common\controller\Api;
use Yansongda\Pay\Pay;
use think\Db;

class Index  extends Api
{
    protected $noNeedLogin = ['mechanismIndexCourses','firmInfo','vipPrice'];
    protected $noNeedRight = ['*'];

    /**
     * 首页推荐机构课程
     */
    public function mechanismIndexCourses()
    {
        $limit = $this->request->post('limit') ?? 10;
        $map = [];
        $map['is_index'] = ['=', 1];
        $list = MechanismCourse::where($map)->order('id desc')->paginate($limit)->toArray();
        foreach ($list['data'] as $key => $value) {

            $list['data'][$key]['zhuanye']=Db::name('mechanism_category')->where('id',$value['mechanism_category_top'])->value('name');
            $list['data'][$key]['jibie']=Db::name('mechanism_category')->where('id',$value['mechanism_category_two'])->value('name');
            $list['data'][$key]['user_check']=Db::name('user_check')->where('id',$value['user_check_id'])->value('name');


        }
        $this->success('成功', $list);
    }

    /**
     * 公司简介
     */
    public function firmInfo()
    {
        $info = FirmInfo::find();
        $culture = FirmCulture::order('weigh asc')->select();
        $course = FirmCourse::order('year desc')->group('year')->select();
        $data = [];
        foreach ($course as $key => $value) {
            $list = FirmCourse::where('year', $value['year'])->order('month desc,weigh asc')->select();
            $arr = ['year'=>$value['year'],'list'=>$list];
            array_push($data, $arr);
        }
        $this->success('成功', ['info'=>$info,'culture'=>$culture,'course'=>$data]);
    }

    /**
     * 购买时间列表
     */
    public function vipPrice()
    {
        $list = VipPrice::order('weigh asc')->select();
        $this->success('成功', $list);
    }

    /**
     * vip生成订单
     */
    public function vipOrderCreate()
    {
        $userModel = $this->auth->getUser();
        $id = $this->request->post('id'); //价格ID
        $vipPrice = VipPrice::where('id', $id)->find();
        if (!$vipPrice) $this->error('请选择购买时间');
        if ($vipPrice->price <= 0) $this->error('请确认支付金额');
        //生成订单
        $orderModel = new VipOrder();
        $orderModel->order_no = createOrderNo();
        $orderModel->user_id = $userModel->id;
        $orderModel->total_amount = $vipPrice->price;
        $orderModel->title = $vipPrice->title;
        $orderModel->month = $vipPrice->month;
        $orderModel->save();
        $this->success('成功', ['order_no'=>$orderModel->order_no]);
    }



    /**
     * 生成支付参数
     */
    public function vipOrderPay()
    {
        $userModel = $this->auth->getUser();
        $order_no = $this->request->post('order_no'); //订单编号
        $type = $this->request->post('type'); //支付方式:alipay=支付宝PC,wechat=微信PC,wechat_mini=微信小程序
        $orderModel = VipOrder::where('order_no', $order_no)->where('user_id', $userModel->id)->find();
        if (!$orderModel) $this->error('订单不存在');
        if (!$type) $this->error('请选择支付方式');
        if ($orderModel->status == 1) $this->error('订单已支付');

        $params = [
            'amount'=>$orderModel['total_amount'],
            'orderid'=>$orderModel['order_no'],
            'type'=>$type,
            'title'=>'购买'.$orderModel['title'],
            'notifyurl'=>request()->root(true).'/api/v1/notify/vip_order_notify/paytype/'.$type, //回调地址
            'returnurl'=>'',
            'method'=>'scan',
            'openid'=>"wx5049d19335c00bd7",
        ];

        $wxret = \addons\epay\library\Service::submitOrder($params);
        if($type=='alipay'){
            $r= (new \app\api\controller\courses\Index())->inviteshopQrcode($wxret['qr_code'],$type,$orderModel['order_no']);
        }elseif($type=='wechat'){
            $r= (new \app\api\controller\courses\Index())->inviteshopQrcode($wxret['code_url'],$type,$orderModel['order_no']);
        }
        $this->success('OK',$r);
    }
}